﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace GameStateManagement.Mine
{
    public class MyButton : GameStateManagement.CommandEntry
    {
        #region Initialization


        public string TextureName { get; set; }
        Texture2D texture;
        Rectangle SourceRectangle;
        public Texture2D Texture
        {
            get { return texture; }
            set { texture = value; }
        }
        public MyButton(string text) : base(text)
        {
        }
        

        #endregion

        #region Update and Draw


        public override void Load(GameScreen screen)
        {
            ContentManager content = screen.ScreenManager.Game.Content;
            this.Texture = content.Load<Texture2D>(this.TextureName);
            
        }


        public override void Update(GameScreen screen, bool isSelected, GameTime gameTime)
        {

            float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 4;
            if (TransitionEnabled)
            {
                if (isSelected)
                    selectionFade = Math.Min(selectionFade + fadeSpeed, 1);
                else
                    selectionFade = Math.Max(selectionFade - fadeSpeed, 0);
            }
        }



        public override void Draw(GameScreen screen, bool isSelected, GameTime gameTime)
        {
            this.SourceRectangle = isSelected ? new Rectangle(0, 0, Texture.Width, Texture.Height / 2 - 2) :
                    new Rectangle(0, Texture.Height / 2, Texture.Width, Texture.Height / 2);


            // Pulsate the size of the selected menu entry.
            double time = gameTime.TotalGameTime.TotalSeconds;

            float pulsate = (float)Math.Sin(time * 6) + 1;

            float scale = 1 - pulsate * 0.05f * selectionFade;

       
            // Draw text, centered on the middle of each line.
            ScreenManager screenManager = screen.ScreenManager;
            SpriteBatch spriteBatch = screenManager.SpriteBatch;
            SpriteFont font = screenManager.Font;

            Position = new Vector2(Position.X + GetWidth(null) / 2, Position.Y);

            //Vector2 origin = new Vector2(0, font.LineSpacing / 2);
            Vector2 origin = new Vector2(GetWidth(null) / 2, GetHeight(null) / 4);
            //spriteBatch.DrawString(font, text, position, color, 0,
                                   //origin, scale, SpriteEffects.None, 0);

            
            spriteBatch.Draw(this.texture, this.Position, this.SourceRectangle, Color.White, 0,
                                   origin, scale, SpriteEffects.None, 0);
        }



        public override int GetHeight(GameScreen screen)
        {
            return this.texture.Height / 2;
        }




        public override int GetWidth(GameScreen screen)
        {
            return this.texture.Width;
        }


        #endregion

    }
}
